package com.d0318.Z18;

public class Tickets implements Runnable{
    int tickets=20;

    @Override
    public void run() {
        while(tickets>=0){
            synchronized (this){
                System.out.println(Thread.currentThread().getName()+"售出:"+tickets+"号");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                tickets--;
                System.out.println("剩余票数:"+tickets);
                if(tickets==0){
                    System.out.println("票卖完了");
                    System.exit(0);
                }
            }
        }
    };

    public static void main(String[] args) {
        Tickets t=new Tickets();
        Thread t1=new Thread(t,"窗口1");
        Thread t2=new Thread(t,"窗口2");
        Thread t3=new Thread(t,"窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}
